package com.api.action;

import com.api.view.config.MIndexConfig;
import com.base.api.ApiBaseController;
import com.base.api.annotation.ApiMethod;
import com.base.dao.model.BFile;
import com.base.service.BFileService;
import com.base.util.DateUtil;
import com.item.ConstantsCode;
import com.item.SystemConst;
import com.item.service.CodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping(value="/api/sys", produces = {"application/json;charset=UTF-8"})
@Api(tags = "系统接口")
public class ApiSystemController extends ApiBaseController{

    @Autowired
    private CodeService codeService;
    @Autowired
    private BFileService fileService;

    @ApiMethod
    @ApiOperation(value = "系统配置", notes = "缓存在本地")
    @RequestMapping(value = "/config",method = {RequestMethod.POST})
    public MIndexConfig indexConfig(){
        Date now = new Date();
        MIndexConfig config = new MIndexConfig();
        //获取系统所有可用配置
        config.setUserRegistProtocol(ConstantsCode.SERVER_URL+"singlePage/"+ SystemConst.REGIST_PAGE);
        config.setUserGetDistance(codeService.getCode(SystemConst.FICITIOUS_GET_POINT));
        //
        String goodsTimeOpen = codeService.getCode(SystemConst.GOODS_TIME_OPEN);
        config.setBxIsOpen("1".equals(goodsTimeOpen)?1:0);
        config.setActIsOpen(0);
        //活动是否公告
        if ("1".equals(codeService.getCode(SystemConst.INDEX_ACTIVITY_OPEN))){
            //开启活动
            //判断活动时间
            Date begin = DateUtil.strToDate(codeService.getCode(SystemConst.INDEX_ACTIVITY_BEGIN),DateUtil.YYSS);
            Date end = DateUtil.strToDate(codeService.getCode(SystemConst.INDEX_ACTIVITY_END),DateUtil.YYSS);
            if (begin != null && end != null){
                if (begin.compareTo(now) <= 0 && end.compareTo(now) >= 0){
                    //开启活动
                    config.setActIsOpen(1);
                }
            }else if (begin == null && end == null){
                config.setActIsOpen(1);
            }
        }
//        config.setActIsOpen("1".equals(codeService.getCode(SystemConst.INDEX_ACTIVITY_OPEN))?1:0);
        //活动开启
        if (config.getActIsOpen() == 1){
            config.setActImg(codeService.getCode(SystemConst.INDEX_ACTIVITY_IMG));
            BFile actImg = fileService.selectByPrimaryKey(config.getActImg());
            if (actImg != null){
                config.setActImgMime(actImg.getFileMinitype());
            }
            if ("2".equals(codeService.getCode(SystemConst.INDEX_ACTIVITY_TYPE))){
                config.setActUrl(ConstantsCode.SERVER_URL+"singlePage/"+ SystemConst.INDEX_ACTIVITY_PAGE);
            }else{
                config.setActUrl(codeService.getCode(SystemConst.INDEX_ACTIVITY_URL));
            }
        }

        config.setUserPushImg(codeService.getCode(SystemConst.USER_PUSH_IMG));
        BFile file = fileService.selectByPrimaryKey(config.getUserPushImg());
        if (file != null){
            config.setUserPushImgMime(file.getFileMinitype());
        }
        config.setStoreDistance(codeService.getCode(SystemConst.STORE_DISTANCE));
        config.setUserDistance(codeService.getCode(SystemConst.FICITIOUS_POINT));
        config.setUserPushDistance(codeService.getCode(SystemConst.USER_PUSH_DISTANCE));
        //用户体力
        config.setMaxStrength(codeService.getCode(SystemConst.USER_STRENGTH_MAX));
        config.setStrengthSeconds(codeService.getCode(SystemConst.USER_STRENGTH_TIME));
        config.setCoinAudio(codeService.getCode(SystemConst.COIN_AUDIO));
        config.setOtherAudio(codeService.getCode(SystemConst.OTHER_AUDIO));
        config.setServiceTel(codeService.getCode(SystemConst.SERVICE_TEL));
        return config;
    }



}
